# math_functions.py

def print_multiplication_table():
    """输出9x9乘法口诀表"""
    for i in range(1, 10):
        for j in range(1, i + 1):
            print(f"{j}*{i}={i * j}", end="\t")
        print()


def prime_factorization(num):
    """输入一个任意正整数，输出分解质因数"""
    factors = []
    d = 2
    while num > 1:
        while num % d == 0:
            factors.append(d)
            num //= d
        d += 1
    result = " * ".join(map(str, factors))
    print(f"{num}={result}")


def process_number():
    """
    输入一个不多于6位的正整数，求它是几位数，逆序打印出各位数字
    """
    num = input("请输入一个不多于6位的正整数:")
    num_digits = len(num)
    print(f"该数是{num_digits}位数")
    print(num[::-1])


def sum_series():
    """
    求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字，个数由键盘控制
    """
    a = int(input("请输入一个数字a："))
    n = int(input("请输入相加的项数n："))
    s = 0
    for i in range(1, n + 1):
        term = int(str(a) * i)
        s += term
    print(s)


def fraction_sum():
    """
    求分数序列：2/1，3/2，5/3，8/5，13/8，21/13...的前N项之和，N由键盘输入
    """
    n = int(input("请输入项数N："))
    sum_frac = 0
    numerator = 2
    denominator = 1
    for _ in range(n):
        sum_frac += numerator / denominator
        new_numerator = numerator + denominator
        denominator = numerator
        numerator = new_numerator
    print(sum_frac)


def find_number():
    """
    一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，求该数
    """
    for num in range(-1000, 10000):
        n1 = int((num + 100) ** 0.5)
        n2 = int((num + 100 + 268) ** 0.5)
        if n1 ** 2 == num + 100 and n2 ** 2 == num + 100 + 268:
            print(num)


def rabbit_count():
    """
    计算每个月的兔子总数，兔子繁殖规律给定
    """
    month = int(input("请输入要计算到第几个月："))
    if month < 1:
        print("月份需大于等于1")
        return
    if month == 1 or month == 2:
        print(1)
    else:
        a, b = 1, 1
        for _ in range(3, month + 1):
            a, b = b, a + b
        print(b)


def matrix_sum():
    """
    输入一个3x3矩阵，求矩阵对角线元素之和
    """
    matrix = []
    print("请逐行输入3x3矩阵的元素，每行元素以空格分隔：")
    for _ in range(3):
        row = list(map(int, input().split()))
        matrix.append(row)
    sum_diagonal = 0
    for i in range(3):
        sum_diagonal += matrix[i][i] + matrix[i][2 - i]
    print(sum_diagonal)


def shift_numbers():
    """
    有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数
    """
    n = int(input("请输入整数的个数n："))
    m = int(input("请输入移动的位置数m："))
    numbers = []
    print("请依次输入这n个整数：")
    for _ in range(n):
        numbers.append(int(input()))
    m %= n
    numbers = numbers[-m:] + numbers[:-m]
    print("移动后的序列为：", numbers)


def sum_fractions_by_odd_even():
    """
    输入n为偶数时，求1/2+1/4+...+1/n，当输入n为奇数时，求1/1+1/3+...+1/n
    """
    n = int(input("请输入整数n："))
    sum_frac = 0
    step = 2 if n % 2 == 0 else 1
    for i in range(1, n + 1, step):
        sum_frac += 1 / i
    print(sum_frac)